<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :labelWidth="110"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" @click="saveDialog" type="primary">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addBrand, updateBrand } from '@/api/common/cars/brandcarsModels';
import { getUserInfo } from '@/api/login';
import { testText64,systemModeCode } from '@/utils/validate';
import Cookies from 'js-cookie';
export default {
  name: 'BrandModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '保 存' };
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.formList.find(item=>item.fieldName == "isVehicleSalesBrand").initialValue =10041002
      if (this.type === 'add'){
          this.formList.find(item=>item.fieldName == "brandCode").disabled=false
      }else if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.brandId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      };
      getUserInfo().then(res=>{
        if(res.resultCode == 200){
          if(res.data.userInfo.dataType == 10461001){
            this.formList[3].initialValue = 10451001;
          }else{
            this.formList[3].initialValue = 10451003;
          }
        }else{
          this.$message.error('获取当前登录用户失败!');
        }
      })
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '品牌代码',
          fieldName: 'brandCode',
          placeholder: '请输入...',
          disabled:true,
          maxlength:64,
          rules: [
            { required: true, message: '请输入品牌代码',  trigger: 'blur'},
            { validator: systemModeCode, trigger: 'blur'}
          ]
        },
        {
          type: 'INPUT',
          label: '品牌名称',
          fieldName: 'brandName',
          placeholder: '请输入...',
          maxlength:64,
          // validator:testText64
          rules: [{ required: true, message: '请输入品牌名称',  trigger: 'blur', }]
        },
        {
          type: 'SELECT',
          label: '是否整车销售',
          fieldName: 'isVehicleSalesBrand',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          disabled:true,
          rules: [{ required: true, message: '请选择整车销售', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '请选择',
          disabled: true,
          itemList: [{ value: 10451001, text: '店面' }, { value: 10451003, text: '厂家' }],
          rules: [{ required: true, message: '请选择数据来源', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择有效性', trigger: 'change' }]
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.recordVersion = this.rowData.recordVersion
        let res = {};
        if (this.type === 'add') {
          res = await addBrand(this.saveData);
        }
        if (this.type === 'edit') {
          res = await updateBrand(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
